require(['router-map', 'filter', 'components/banner', 'components/footer', 'components/left', 'components/window'], function(RouterMap, Filter, Banner, Footer, Left, Window) {

  Vue.http.headers.common.Authorization = 'Basic YXBpOnBhc3N3b3Jk';
  //  Vue.http.options.emulateJSON = true

  var router = new VueRouter({
    linkActiveClass: 'active'
  });

  var data = {
    user: {
      isLogin: false,
      lastLoginTime: Date.now(),
      telphone: '',
      qq: '',
      userName: '',
      uid: '',
      avatar: '',
      account: '',
      message: ''
    },
    small: false,
    box: {
      placeholder: '',
      val: '',
      type: '',
      name: false,
      data: '',
      callback: function(){}
    },
    categorys: []
  };
  Vue.http.post('/user/info').then(function(res) {
    res = res.data;
    if (res.code === 100) {
      //  用户未登录
      data.user.isLogin = false;
      return;
    } else if (res.code !== 0) {
      data.user.isLogin = false;
      alert(res.message);
      return;
    }
    var ds = res.data;
    for(var i in ds){
      data.user[i] = ds[i];
    }
    data.user.isLogin = true;
  });

  Filter(Vue);

  var app = Vue.extend({
    data: function() {
      return data;
    },
    methods: {},
    ready: function() {
      getCategory(this)
    },
    components: {
      'app-banner': Banner,
      'app-left': Left,
      'app-footer': Footer,
      'app-window': Window
    }
  });

  router.map(RouterMap);
  router.start(app, 'body');

  router.beforeEach(function(transition) {
    var to = transition.to;
    if (to.mustLogin === true && data.user.isLogin === false) {
      router.replace({
        name: 'index'
      });
    }
    transition.next();
  });
  window.onscroll = onscroll;

  function onscroll() {
    if (window.scrollY > 100) {
      if (data.small === false) {
        data.small = true;
      }
    } else {
      if (data.small === true) {
        data.small = false;
      }
    }
  }

  function getCategory(vm) {
    vm.$http.post('/category/get').then(function(res) {
      res = res.data;
      if (res.code !== 0) {
        console.log(res.message);
        return;
      }
      vm.categorys = res.data;
    });
  }
});